﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.BLL.InfoConfig;
using MINERP.MODEL.InfoConfig;

namespace MINERP.InfoConfig
{
    public partial class FrmProductOutShopManager :Form
    {
        private int productid = 0;
        ProductOutShopBLL productOutShopBLL = new ProductOutShopBLL();
        private static FrmProductOutShopManager _instance =null;
        public static FrmProductOutShopManager GetInstance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new FrmProductOutShopManager();
                }
                return _instance;
            }
        }
        private FrmProductOutShopManager()
        {
            InitializeComponent();
        }
        public FrmProductOutShopManager(int productid)
        {
            this.productid = productid;
            InitializeComponent();
        }
        private void FrmProductOutShopManager_Load(object sender, EventArgs e)
        {
            LinkProductShop();
        }

        private void LinkProductShop()
        {
            gridControlProductOutShop.DataSource = productOutShopBLL.GetProductOutShopList(productid);
        }

        private void FrmProductOutShopManager_FormClosing(object sender, FormClosingEventArgs e)
        {
            _instance = null;
        }
        private void gridViewProductOutShop_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
           
            if (e.Column.FieldName == "IsDefaultOutShop")
            {
                if ((bool)e.Value)
                {
                    List<ProductOutShopInfo> list = (List<ProductOutShopInfo>)gridViewProductOutShop.DataSource;
                    ProductOutShopInfo posInfo = (ProductOutShopInfo)gridViewProductOutShop.GetFocusedRow();
                    posInfo.IsDefaultOutShop = true;
                    list = list.Where(l => l.ProductId == posInfo.ProductId&&l.OutShopId !=posInfo.OutShopId).ToList();
                    foreach (var item in list)
                    {
                        item.IsDefaultOutShop = false;
                    }
                    gridViewProductOutShop.RefreshData();
                    
                }
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                List<ProductOutShopInfo> list = (List<ProductOutShopInfo>)gridViewProductOutShop.DataSource;
                productOutShopBLL.Update(list);
                MessageBox.Show("保存成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }
    }
}
